$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$("#cbatchId").val($("#cbatchId").attr('data-id'));
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationBatch/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入考试批次名称"
			}
		}
	})
}

layui.use('laydate', function(){

	var laydate = layui.laydate;

	laydate.render({
		elem: '#startTime'
		,type: 'datetime'
	});

	laydate.render({
		elem: '#endTime'
		,type: 'datetime'
	});

})

$(function (){

	$("#cbatchId").focus(function (){
		$("#cbatchIdList").show();
	})

	$("#cbatchId").on('blur',function (){
		setTimeout(function (){
			$("#cbatchIdList").hide();
		},150);
	})

	$("#cbatchId").on("input",function (ev){
		cbatchId();
	})

	cbatchId()
	function cbatchId(){
		$.get('/jzweb/examinationBatch/certificateBatchList',{
			name:$("#cbatchId").val()},
			function (r){
				let str = ``;
				r.data.forEach(function (v){
					str += `<li data-id="${v.id}" title="${v.name}"> ${v.name} </li>`
				})
				$("#cbatchIdList").html( str );

				$("#cbatchIdList").find('li').click(function (){
					$("#cbatchId").val($(this).html()).attr('data-id',$(this).attr('data-id'));
					$("#cbatchIdList").hide();
				})
			}
		)
	}

})